package com.javakc.pms.dispord.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javakc.commonutils.api.R;
import com.javakc.pms.dispord.entity.DispOrd;

import com.javakc.pms.dispord.service.DispOrdService;
import com.javakc.pms.dispord.vo.DispOrdData;
import com.javakc.pms.dispord.vo.DispOrdQuery;
import com.javakc.pms.listener.ExcelListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "调度指令库管理")
@RestController
@RequestMapping("/pms/dispord")   // 对应前端的调用方法
@CrossOrigin     // 跨域
public class DispOrdController {

    @Autowired
    private DispOrdService dispOrdService;

    @ApiOperation(value = "查询所有指令库")
    @GetMapping
    public R findAll() {
//        int i = 1/0;
        List<DispOrd> list = dispOrdService.list();
        return R.OK().data("items", list);  // 数据ok返回的true 单独设置data
    }

    //带条件的分页查询
    @ApiOperation(value = "根据条件进行分页查询 - 调度指令库")
    @PostMapping("{pageNo}/{pageSize}")
    public R findPageDispOrd(
            @ApiParam(name = "pageNo", value = "页码", required = true)
            @PathVariable("pageNo") Long pageNo,
            @ApiParam(name = "pageSize", value = "每页最大条数", required = true)
            @PathVariable("pageSize") Long pageSize,
            @ApiParam(name = "dispOrdQuery", value = "查询条件封装对象", required = false)
            @RequestBody(required = false) DispOrdQuery dispOrdQuery) {

        Page<DispOrd> dispOrdPage = new Page<>(pageNo, pageSize);

        dispOrdService.findPageDispOrd(dispOrdPage, dispOrdQuery);  //条件
        long total = dispOrdPage.getTotal();
        List<DispOrd> dispOrdList = dispOrdPage.getRecords(); //返回的数据
        return R.OK().data("total", total).data("items", dispOrdList);
    }

    @ApiOperation(value = "新增调度指令库")   //有true 和 false
    @PostMapping("saveDispOrd")
    public R saveDispOrd(@RequestBody DispOrd dispOrd) {
        boolean flag = dispOrdService.save(dispOrd);
        if (flag)
            return R.OK();
        else
            return R.ERROR();
    }

    @ApiOperation(value = "根据 ID 获取调度指令库")
    @GetMapping("getDispOrdById/{dispOrdId}")
    public R getDispOrdById(@PathVariable("dispOrdId") String dispOrdId) {
        DispOrd dispOrd = dispOrdService.getById(dispOrdId);
        return R.OK().data("dispOrd", dispOrd);   //data对象中有dispOrd属性 {}
    }

    @ApiOperation(value = "修改调度指令库")
    @PutMapping("updateDispOrd")
    public R updateDispOrd(@RequestBody DispOrd dispOrd) {
        boolean flag = dispOrdService.updateById(dispOrd);
        if (flag)
            return R.OK();
        else
            return R.ERROR();
    }

    @ApiOperation(value = "根据 ID 删除调度指令库")
    @DeleteMapping("deleteDispOrdById/{dispOrdId}")
    public R deleteDispOrdById(@PathVariable("dispOrdId") String dispOrdId) {
        boolean flag = dispOrdService.removeById(dispOrdId);
        if (flag)
            return R.OK();
        else
            return R.ERROR();
    }

    @ApiOperation(value = "列表导出", notes = "使用阿里EasyExcel导出Excel格式的列表数据")
    @GetMapping("exportEasyExcel")
    public void exportEasyExcel(HttpServletResponse response) {
        // ## 查询调度指令库
        List<DispOrd> dispOrdList = dispOrdService.list();
        // ## 定义导出列表集合
        List<DispOrdData> dispOrdDataList = new ArrayList<>();

        for (DispOrd dispOrd : dispOrdList) {
            DispOrdData dispOrdData = new DispOrdData();
            BeanUtils.copyProperties(dispOrd, dispOrdData);
            dispOrdDataList.add(dispOrdData);
        }
        String fileName = "dispOrdList";

        try {
            // ## 设置响应信息
            response.reset();
            response.setContentType("application/vnd.ms-excel; charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8") + ".xlsx");
            EasyExcel.write(response.getOutputStream(), DispOrdData.class).sheet("指令列表").doWrite(dispOrdDataList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /** 导入功能
     *
     * @param file
     */
    @ApiOperation(value = "列表导入", notes = "使用阿里EasyExcel导入Excel格式的列表数据")
    @PostMapping("importEasyExcel")
    public void importEasyExcel(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), DispOrdData.class, new ExcelListener(dispOrdService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
